﻿<%@ WebHandler Language="C#" Class="HandlerReport" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.SessionState;
using Entity;
using PaymentBLL;
using HighLowGameBLL;

public class HandlerReport : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        string type = context.Request.QueryString["t"] ?? string.Empty;

        switch (type)
        {

            case "rptPaymentIn":
                rptPaymentIn(context);
                break;
            case "rptPaymentOut":
                rptPaymentOut(context);
                break;
            case "rptGeneral":
                rptGeneral(context);
                break;
            case "rptUserAssets":
                rptUserAssets(context);
                break;
            case "rptInGame":
                rptInGame(context);
                break;
            case "rptMapInGame":
                rptMapInGame(context);
                break;
        }
    }


    public void rptPaymentIn(HttpContext context)
    {
        var ctr = new CtrLogPaymentGatewayIn();
        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        var fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        var toDate = (DateTime)Library.Lib.GetDateTime(todate);
        int typeId = Convert.ToInt32(context.Request.QueryString["typeId"] ?? "-1");
        int accountId = Convert.ToInt32(context.Request.QueryString["accountId"] ?? "-1");
        string accountName = context.Request.QueryString["accountName"] ?? "";
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "10");
        int? totalRecord = 0;
        int? totalVndIn = 0;
        int? totalReceivAu = 0;
        int? totalReceivAg = 0;
        var lst = ctr.SelectLogPaymentGatewayInBy(-1, fromDate, toDate, accountId, -1, accountName, typeId, "", "", ref totalVndIn,
                                       ref totalReceivAu, ref totalReceivAg, currentPage, pageSize, ref totalRecord);

        var lstGate = ctr.StatisticsLogPaymentGatewayInSum(-1, fromDate, toDate, accountId, typeId, accountName);
        
        var obj = new
        {
            Items = lst,
            TotalRecord = totalRecord ?? 0,
            TotalVndIn = totalVndIn ?? 0,
            TotalReceivAu = totalReceivAu ?? 0,
            TotalReceivAg = totalReceivAg ?? 0,
            Gates=lstGate
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void rptPaymentOut(HttpContext context)
    {
        var ctr = new CtrLogPaymentGatewayOut();
        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        var fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        var toDate = (DateTime)Library.Lib.GetDateTime(todate);
        int typeId = Convert.ToInt32(context.Request.QueryString["typeId"] ?? "-1");
        int accountId = Convert.ToInt32(context.Request.QueryString["accountId"] ?? "-1");
        string accountName = context.Request.QueryString["accountName"] ?? "";
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "10");
        int? totalRecord = 0;
        int? totalVndOut = 0;
        int? totalAuOut = 0;
        int? totalAgOut = 0;
        var lst = ctr.SelectLogPaymentGatewayOutBy(-1, fromDate, toDate, accountId, -1, accountName, typeId, "", "",
                                       ref totalAuOut, ref totalAgOut, ref totalVndOut, currentPage, pageSize, ref totalRecord);
        var lstGate = ctr.StatisticsLogPaymentGatewayOutSum(-1, fromDate, toDate, accountId, typeId, accountName);
        var obj = new
        {
            Items = lst,
            TotalRecord = totalRecord ?? 0,
            TotalAuOut = totalAuOut ?? 0,
            TotalAgOut = totalAgOut ?? 0,
            TotalVndOut = totalVndOut ?? 0,
            Gates = lstGate
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    public void rptGeneral(HttpContext context)
    {

        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        var fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        var toDate = (DateTime)Library.Lib.GetDateTime(todate);
        var ctrIn = new CtrLogPaymentGatewayIn();
        var lstIn = ctrIn.StatisticsLogPaymentGatewayIn(fromDate, toDate);

        var ctrOut = new CtrLogPaymentGatewayOut();
        var lstOut = ctrOut.StatisticsLogPaymentGatewayOut(fromDate, toDate);

        var ctrS = new CtrLogHiLoSession();
        var lstS = ctrS.StatisticsLogHiLoSession(fromDate, toDate);

        var ctrC = new CtrLogConvertAuAg();
        long? fromAg = 0;
        long? toAu = 0;
        ctrC.StatisticsLogConvertAgToAu(ref fromAg, ref toAu);
        var obj = new
        {
            In = lstIn,
            Out = lstOut,
            Ses = lstS,
            Convert = new
            {
                FromAg = fromAg??0,
                ToAu = toAu??0
            }
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void rptUserAssets(HttpContext context)
    {

        int accId = Convert.ToInt32(context.Request.QueryString["accountId"] ?? "-1");
        string accName = context.Request.QueryString["accountName"] ?? "";

        var ctrA = new CtrAccount();
        long? totalVndIn = 0;
        long? totalReceiveAu = 0;
        long? totalReceiveAg = 0;
        long? totalAuOut = 0;
        long? totalAgOut = 0;
        long? totalVndOut = 0;
        long? totalAuPlay = 0;
        long? totalAgPlay = 0;
        long? totalAuBonus = 0;
        long? totalAgBonus = 0;
        long? totalAgToAu = 0;
        long? totalAuFromAg = 0;
        int? currentAu = 0;
        int? currentAg = 0;
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "-1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "-1");
        int? totalRecord = 0;
        var lst = ctrA.SelectAssetsStatistics(accId, accName, ref totalVndIn, ref totalReceiveAu, ref totalReceiveAg, ref totalAuOut, ref totalAgOut, ref totalVndOut, ref totalAuPlay, ref totalAgPlay, ref totalAuBonus, ref totalAgBonus, ref totalAgToAu, ref totalAuFromAg, ref currentAu, ref  currentAg, currentPage, pageSize, ref totalRecord);
        var obj = new
        {
            Items = lst,
            TotalVndIn = totalVndIn ?? 0,
            TotalReceiveAu = totalReceiveAu ?? 0,
            TotalReceiveAg = totalReceiveAg ?? 0,
            TotalAuOut = totalAuOut ?? 0,
            TotalAgOut = totalAgOut ?? 0,
            TotalVndOut = totalVndOut ?? 0,
            TotalAuPlay = totalAuPlay ?? 0,
            TotalAgPlay = totalAgPlay ?? 0,
            TotalAuBonus = totalAuBonus ?? 0,
            TotalAgBonus = totalAgBonus ?? 0,
            TotalAgToAu = totalAgToAu ?? 0,
            TotalAuFromAg = totalAuFromAg ?? 0,
            CurrentAu = currentAu ?? 0,
            CurrentAg = currentAg ?? 0,
            TotalRecord = totalRecord ?? 0
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    public void rptMapInGame(HttpContext context)
    {
        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        var fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        var toDate = (DateTime)Library.Lib.GetDateTime(todate);
        int typeId = Convert.ToInt32(context.Request.QueryString["typeId"] ?? "-1");
        int accountId = Convert.ToInt32(context.Request.QueryString["accountId"] ?? "-1");
        int sessionStatus = Convert.ToInt32(context.Request.QueryString["sessionStatus"] ?? "-1");
        int betUnit = Convert.ToInt32(context.Request.QueryString["betUnit"] ?? "-1");
        int finishStatus = Convert.ToInt32(context.Request.QueryString["finishStatus"] ?? "-1");
        string accountName = context.Request.QueryString["accountName"] ?? "";

        var ctr = new CtrLogHiLoSession();
        var lst = ctr.SelectHiLoSessionRoundStatistics(fromDate, toDate, accountId, typeId, accountName, sessionStatus, betUnit,
                                         finishStatus);
        var lst1 = new List<uspLogHiLoSessionRoundStatisticsByResult>();
        foreach (var item in lst)
        {
            var item1 = new uspLogHiLoSessionRoundStatisticsByResult();
            item1.TotalCollect = (Int32)Math.Round((double)((1.0*item.TotalCollect / item.TotalSession) * 100));
            item1.TotalExtraLife = (Int32)Math.Round((double)((1.0 * item.TotalExtraLife / item.TotalSession) * 100));
            item1.TotalFreeSpin = (Int32)Math.Round((double)((1.0 * item.TotalFreeSpin / item.TotalSession) * 100));
            item1.TotalJacpot = (Int32)Math.Round((double)((1.0 * item.TotalJacpot / item.TotalSession) * 100));
            item1.TotalLost = (Int32)Math.Round((double)((1.0 * item.TotalLost / item.TotalSession) * 100));
            item1.TotalNudges = (Int32)Math.Round((double)((1.0 * item.TotalNudges / item.TotalSession) * 100));
            item1.TotalOutOf = (Int32)Math.Round((double)((1.0 * item.TotalOutOf / item.TotalSession) * 100));
            item1.TotalSession = (Int32)Math.Round((double)((1.0 * item.TotalSession / item.TotalSession) * 100));
            item1.Round = item.Round;
            lst1.Add(item1);        
        }
        var obj = new
        {
            Items = from item in lst
                    orderby item.Round ascending 
                    select item,
            ItemsPer = from item in lst1
                       orderby item.Round ascending
                       select item
        };
        
        
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void rptInGame(HttpContext context)
    {
        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        var fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        var toDate = (DateTime)Library.Lib.GetDateTime(todate);
        int typeId = Convert.ToInt32(context.Request.QueryString["typeId"] ?? "-1");
        int accountId = Convert.ToInt32(context.Request.QueryString["accountId"] ?? "-1");
        int sessionStatus = Convert.ToInt32(context.Request.QueryString["sessionStatus"] ?? "-1");
        int betUnit = Convert.ToInt32(context.Request.QueryString["betUnit"] ?? "-1");
        int finishStatus = Convert.ToInt32(context.Request.QueryString["finishStatus"] ?? "-1");
        string accountName = context.Request.QueryString["accountName"] ?? "";
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "10");
        int? totalRecord = 0;

        long? totalAuBet = 0;
        long? totalAgBet = 0;
        long? totalAuCollect = 0;
        long? totalAgCollect = 0;

        var ctr = new CtrLogHiLoSession();
        var lst = ctr.SelectHiLoSessionBy(fromDate, toDate, accountId, typeId, accountName, sessionStatus, betUnit,
                                         finishStatus, ref totalAuBet, ref totalAgBet, ref totalAuCollect, ref totalAgCollect,
                                         currentPage, pageSize, ref totalRecord);
        var obj = new
        {
            Items = lst,
            TotalAuBet = totalAuBet ?? 0,
            TotalAgBet = totalAgBet ?? 0,
            TotalAuCollect = totalAuCollect ?? 0,
            TotalAgCollect = totalAgCollect ?? 0,
            TotalRecord = totalRecord ?? 0
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}